package com.gzyj.jtt808.v2011.additionalinfo;



import com.gzyj.jtt808.utils.HexBytesUtil;
import com.gzyj.jtt808.v2011.enums.addtionalinfo.JT808_Addtional_Direction;
import com.gzyj.jtt808.v2011.enums.addtionalinfo.JT808_Addtional_Position;
import io.netty.buffer.ByteBuf;
import lombok.Data;

import java.io.Serializable;

/**
 * 进出区域_路线报警附加信息
 *
 * @author lhj
 */
@Data
public class JTT808_0x0200_0x12 extends JTT808_AddtionalInfo implements Serializable {

    private JT808_Addtional_Position PositionType;
    private int AreaId;//区域或路段ID
    private JT808_Addtional_Direction Direction;//方向

    public JTT808_0x0200_0x12() {
        this.setID(0x12);
        this.setLength(6);
    }

    @Override
    public void encode(ByteBuf buf) {
        buf.writeByte(this.getID());
        buf.writeByte(6);
        buf.writeByte(this.PositionType.getCode());
        buf.writeInt(this.AreaId);
        buf.writeByte(this.Direction.getCode());
    }

    @Override
    public JTT808_0x0200_0x12 decode(ByteBuf buf) {
        byte[] temp = HexBytesUtil.readByOffset(buf, 1);
        this.setLength(Integer.parseInt(HexBytesUtil.bytesToHex(temp), 16));
        temp = HexBytesUtil.readByOffset(buf, 1);
        this.PositionType = JT808_Addtional_Position.find(Integer.parseInt(HexBytesUtil.bytesToHex(temp), 16));
        temp = HexBytesUtil.readByOffset(buf, 4);
        this.AreaId = HexBytesUtil.byteArrayToInt(temp);
        temp = HexBytesUtil.readByOffset(buf, 1);
        this.Direction = JT808_Addtional_Direction.find(Integer.parseInt(HexBytesUtil.bytesToHex(temp), 16));
        return this;
    }
}
